package com.oglvip.business.web.controller;

import com.google.common.collect.Maps;
import com.oglvip.business.consts.common.ResponseCode;
import com.oglvip.business.dto.PageBean;
import com.oglvip.business.dto.ResponseData;
import com.oglvip.business.remote.service.api.OrderService;
import com.oglvip.business.remote.service.entity.Order;
import com.oglvip.business.remote.service.entity.PageData;
import com.oglvip.business.remote.service.entity.StoreAccount;
import com.oglvip.business.util.SignUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.TreeMap;

/**
 * @auther hanchao
 * @date 2017/2/8 23:27
 */
@RestController
@RequestMapping("/orders")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @RequestMapping("/list")
    public ResponseData pageList(PageBean pageBean,String startTime,String endTime,String orderNo,String orderStatus) throws ParseException {
        if(startTime != null){
            startTime = DateFormatUtils.ISO_DATE_FORMAT.parse(startTime).getTime() + "";
        }
        if(endTime != null){
            endTime = (DateFormatUtils.ISO_DATE_FORMAT.parse(endTime).getTime() + (24*60*60*1000-1)) + "";
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();
        ResponseData<PageData<Order>> resp = orderService.list(account.getTicket(),account.getStoreId(),"".equals(orderStatus)?0:Integer.parseInt(orderStatus),orderNo,startTime,endTime,pageBean.getFirstResult(),pageBean.getOnePageSize());
        PageData pageData = resp.get();
        ResponseData result = new ResponseData();
        result.setResponseCode(ResponseCode.SUCCESS);
        pageBean.setTotalResults(pageData.getSize());

//
//        Order order = new Order();
//        order.setId(1);
//        order.setAddress("add");
//        order.setCreateTime("sadasdasd");
//        order.setOrderNo("no");
//        order.setPhone("1565");
//        order.setStatus(1);
//        order.setStatusDesc("haha");
//        order.setTotalPrice(0.0);
//        pageData.getList().add(order);
//

        pageBean.setData(pageData.getList());
        result.setData(pageBean);
        return result;
    }

    @RequestMapping("/delete")
    public ResponseData delete(String orderNo){
        Long timestamp = System.currentTimeMillis();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();

        TreeMap<String,Object> map = Maps.newTreeMap();
        map.put("ticket",account.getTicket());
        map.put("orderNo",orderNo);
        map.put("timestamp",timestamp);
        String sig = SignUtil.getSign(map);


        return orderService.delete(account.getTicket(),orderNo,timestamp,sig);
    }

    /**
     * @param orderNo
     * @return
     */
    @RequestMapping("/receive")
    public ResponseData receive(String orderNo){
        Long timestamp = System.currentTimeMillis();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();

        TreeMap<String,Object> map = Maps.newTreeMap();
        map.put("ticket",account.getTicket());
        map.put("orderNo",orderNo);
        map.put("timestamp",timestamp);
        String sig = SignUtil.getSign(map);


        return orderService.receive(account.getTicket(),orderNo,timestamp,sig);
    }

    @RequestMapping("/send")
    public ResponseData send(String orderNo){
        Long timestamp = System.currentTimeMillis();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();

        TreeMap<String,Object> map = Maps.newTreeMap();
        map.put("ticket",account.getTicket());
        map.put("orderNo",orderNo);
        map.put("timestamp",timestamp);
        String sig = SignUtil.getSign(map);


        return orderService.send(account.getTicket(),orderNo,timestamp,sig);
    }

    @RequestMapping("/detail")
    public ResponseData detail(String orderNo){
        Long timestamp = System.currentTimeMillis();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();

        TreeMap<String,Object> map = Maps.newTreeMap();
        map.put("ticket",account.getTicket());
        map.put("orderNo",orderNo);
        map.put("timestamp",timestamp);
        String sig = SignUtil.getSign(map);

        return orderService.getDetail(account.getTicket(),orderNo,timestamp,sig);
    }

}
